package com.qianfeng.bob.xiexiaobo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

public class VideoActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private SurfaceView mSurfaceView;
    private SeekBar mSeekBar;
    private MediaPlayer mPlayer;
    private Button mPlayButton;
    private int mPostion;

    private boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video );
        flag = true;
        mSurfaceView = ((SurfaceView) findViewById(R.id.surfaceViewId));
        mSeekBar = ((SeekBar) findViewById(R.id.seekBarId));
        mPlayButton = ((Button) findViewById(R.id.bt_play));
        mPlayer = MediaPlayer.create(this, R.raw.a);
        //获取Surface
        mSurfaceView.getHolder().addCallback(this);
        //固定Surface大小
        mSurfaceView.getHolder().setKeepScreenOn(true);

        //设置起始进度值
        mSeekBar.setProgress(0);

        //设置max
        mSeekBar.setMax(mPlayer.getDuration());

        new PlayProgress().start();//发送广播

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (mSeekBar!=null){
                    mSeekBar.setProgress(mPlayer.getCurrentPosition());//更新进度
                }
            }
        }, new IntentFilter("surface"));
        startOnCreat();
    }

    private void startOnCreat() {
        if(!mPlayer.isPlaying())
        {   mPlayButton.setText("暂停");
            mPlayer.seekTo(mPostion);
            mPlayer.start();//开始播放
        }
    }


    //播放
    public void play(View view)
    {
        if(!mPlayer.isPlaying())
        {   mPlayButton.setText("暂停");
            mPlayer.seekTo(mPostion);
            mPlayer.start();//开始播放
        }else {
            mPlayButton.setText("播放");
            mPostion = mPlayer.getCurrentPosition();//获取当前播放位置
            mPlayer.pause();
        }
    }



    //Surface已经创建
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //借助SurfaceView的Holder来展示图像信息
        mPlayer.setDisplay(mSurfaceView.getHolder());
    }
    //改变了Surface大小的

    /***
     *
     * @param holder  :surface
     * @param format  :格式
     * @param width
     * @param height
     */
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.i("info","调用le ------surfaceChanged-----------");
    }

    //Surface销毁的一个方法
    /**
     *
     * @param holder
     */
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i("info","销毁le ------surfaceDestroyed-----------");

    }

    public class PlayProgress extends Thread{
        @Override
        public void run() {
            while(flag)
            {
                sendBroadcast(new Intent("surface"));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //释放资源
        flag = false;
        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;

    }
}

